package com.wxdc.nmgyt.nmgytwydc.presenter;

import com.wxdc.nmgyt.nmgytwydc.base.RxPersenter;
import com.wxdc.nmgyt.nmgytwydc.http.RetrofitHelper;
import com.wxdc.nmgyt.nmgytwydc.model.BASE_WORKFLOWCURRENT;
import com.wxdc.nmgyt.nmgytwydc.model.JcdInfoBean;
import com.wxdc.nmgyt.nmgytwydc.model.backresult.ValidateJcdInfoResultBean;
import com.wxdc.nmgyt.nmgytwydc.presenter.contract.NavigationDataContract;

import java.util.List;

import javax.inject.Inject;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
import okhttp3.ResponseBody;

/**
 * Created by Administrator on 2018/4/25.
 */

public class NavigationDataPersenter extends RxPersenter<NavigationDataContract.View> implements NavigationDataContract.Persenter {
    private RetrofitHelper mRetrofitHelper;

    @Inject
    public NavigationDataPersenter(RetrofitHelper retrofitHelper) {
        mRetrofitHelper = retrofitHelper;
    }

    @Override
    public void getYdInfo(String ydbh) {

    }

    /**
     * 查询该用户下所有监测点信息
     *
     */
    @Override
    public void getJcdInfo() {
       // Disposable disposable =
                mRetrofitHelper.getJcdInfo()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<JcdInfoBean>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addDisposable(d);
                    }

                    @Override
                    public void onNext(List<JcdInfoBean> jcdInfoBeans) {
                        mView.showJcdInfo(jcdInfoBeans);
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.showError("数据请求失败");
                    }

                    @Override
                    public void onComplete() {

                    }
                });
       /* jczInfos ->
                mView.showJcdInfo(jczInfos), throwable -> {
            mView.showError("数据请求失败...sssss");
        }*/
        //addDisposable(disposable);
    }

    /**
     * @param bean
     * 提交样地反馈信息
     */
    @Override
    public void commitFeedBackInfo(JcdInfoBean bean) {
        mRetrofitHelper.commitFeedBackData(bean)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Observer<ResponseBody>() {
                        @Override
                        public void onSubscribe(Disposable d) {
                            addDisposable(d);
                        }

                        @Override
                        public void onNext(ResponseBody responseBody) {
                            mView.acceptCommitFeedBackInfoResult(responseBody);
                        }

                        @Override
                        public void onError(Throwable e) {
                            mView.showError("提交失败!");
                        }

                        @Override
                        public void onComplete() {

                        }
                    });
    }

    /**
     * 审核数据查询
     * @param id(0:是待审和退回的数据；1：是当前用户审核完成的数据)
     */
    @Override
    public void findAuditData(int id) {
        mRetrofitHelper.getAuditData(id)
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new Observer<List<BASE_WORKFLOWCURRENT>>() {
                            @Override
                            public void onSubscribe(Disposable d) {
                                addDisposable(d);
                            }

                            @Override
                            public void onNext(List<BASE_WORKFLOWCURRENT> base_workflowcurrents) {
                                if (id == 0) {
                                    mView.getBackOrWaitAuditDataResult(base_workflowcurrents);
                                } else if (id == 1) {
                                    mView.getPassAuditDataResult(base_workflowcurrents);
                                } else if (id == 2) {
                                    mView.getAuditDataResult(base_workflowcurrents);
                                }
                            }

                            @Override
                            public void onError(Throwable e) {
                                mView.showError("error");
                            }

                            @Override
                            public void onComplete() {

                            }
                        });
    }

    @Override
    public void validateIsYdInfo(String  gempointID) {
        mRetrofitHelper.validateYdInfo(gempointID)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ValidateJcdInfoResultBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addDisposable(d);
                    }

                    @Override
                    public void onNext(ValidateJcdInfoResultBean validateJcdInfoResultBean) {
                        mView.getYdInfoValidateResult(validateJcdInfoResultBean);
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.showError("error");
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }


}
